package device

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
)

type DeviceService struct{}

// CreateDevice 创建设备信息记录
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) CreateDevice(ctx context.Context, Dev *device.Device) (err error) {
	err = global.GVA_DB.Create(Dev).Error
	return err
}

// DeleteDevice 删除设备信息记录
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) DeleteDevice(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&device.Device{}, "id = ?", ID).Error
	return err
}

// DeleteDeviceByIds 批量删除设备信息记录
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) DeleteDeviceByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]device.Device{}, "id in ?", IDs).Error
	return err
}

// UpdateDevice 更新设备信息记录
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) UpdateDevice(ctx context.Context, Dev device.Device) (err error) {
	err = global.GVA_DB.Model(&device.Device{}).Where("id = ?", Dev.ID).Updates(&Dev).Error
	return err
}

// GetDevice 根据ID获取设备信息记录
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) GetDevice(ctx context.Context, ID string) (Dev device.Device, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&Dev).Error
	return
}

// GetDeviceInfoList 分页获取设备信息记录
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) GetDeviceInfoList(ctx context.Context, info deviceReq.DeviceSearch) (list []device.Device, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&device.Device{})
	var Devs []device.Device
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	if info.DeviceName != nil && *info.DeviceName != "" {
		db = db.Where("device_name LIKE ?", "%"+*info.DeviceName+"%")
	}
	if info.OperateStatus != nil && *info.OperateStatus != "" {
		db = db.Where("operate_status = ?", *info.OperateStatus)
	}
	if info.RunStatus != nil && *info.RunStatus != "" {
		db = db.Where("run_status = ?", *info.RunStatus)
	}
	if info.ActivateStatus != nil && *info.ActivateStatus != "" {
		db = db.Where("activate_status = ?", *info.ActivateStatus)
	}
	if info.AfterSalePerson != nil && *info.AfterSalePerson != "" {
		db = db.Where("after_sale_person LIKE ?", "%"+*info.AfterSalePerson+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&Devs).Error
	return Devs, total, err
}
func (DevService *DeviceService) GetDevicePublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

type DeviceDashboardData struct {
	TotalDevices       int64 `json:"totalDevices"`       // 全部设备数量
	InUseDevices       int64 `json:"inUseDevices"`       // 在用设备数量
	IdleDevices        int64 `json:"idleDevices"`        // 闲置设备数量
	DisabledDevices    int64 `json:"disabledDevices"`    // 停用设备数量
	ActivatedDevices   int64 `json:"activatedDevices"`   // 已激活设备数量
	UnactivatedDevices int64 `json:"unactivatedDevices"` // 未激活设备数量
	NormalDevices      int64 `json:"normalDevices"`      // 正常设备数量
	FaultyDevices      int64 `json:"faultyDevices"`      // 故障设备数量
}

// DeviceDashboard 设备看板
// Author [yourname](https://github.com/yourname)
func (DevService *DeviceService) DeviceDashboard(ctx context.Context) (data DeviceDashboardData, err error) {
	// 请在这里实现自己的业务逻辑
	db := global.GVA_DB.Model(&device.Device{})

	// 统计全部设备数量
	err = db.Count(&data.TotalDevices).Error
	if err != nil {
		return
	}

	// 统计在用设备数量
	err = db.Where("operate_status = ?", "在用").Count(&data.InUseDevices).Error
	if err != nil {
		return
	}

	// 统计闲置设备数量
	err = db.Where("operate_status = ?", "闲置").Count(&data.IdleDevices).Error
	if err != nil {
		return
	}

	// 统计停用设备数量
	err = db.Where("operate_status = ?", "停用").Count(&data.DisabledDevices).Error
	if err != nil {
		return
	}

	// 统计已激活设备数量
	err = db.Where("activate_status = ?", "已激活").Count(&data.ActivatedDevices).Error
	if err != nil {
		return
	}

	// 统计未激活设备数量
	err = db.Where("activate_status = ?", "未激活").Count(&data.UnactivatedDevices).Error
	if err != nil {
		return
	}

	// 统计正常设备数量（在用且已激活且运行状态正常）
	err = db.Where("operate_status = ? AND activate_status = ? AND run_status = ?", "在用", "已激活", "正常").Count(&data.NormalDevices).Error
	if err != nil {
		return
	}

	// 统计故障设备数量（在用且已激活且运行状态故障）
	err = db.Where("operate_status = ? AND activate_status = ? AND run_status = ?", "在用", "已激活", "故障").Count(&data.FaultyDevices).Error
	if err != nil {
		return
	}
	return
}
